package com.biz.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.api.taobao.Props;
import com.biz.jdbc.CatsDao.Cats;
import com.taobao.api.ApiException;
import com.taobao.api.domain.ItemProp;
import com.taobao.api.domain.PropValue;
import com.util.ConnDatabase;
import com.util.DbUtil;

/**
 * @author liuchao
 * @version $Id: PvalueDao.java,v 0.1 2012-9-15 ����12:17:52 liuchao Exp $
 */
public class PvalueDao {

    
    public static final String SQL_INSERT = String.format(
        "insert into %s (%s,%s,%s) values (?,?,?)"  ,
        "props_value",
        "vid",
        "vname",
        "pid"
          );
    
    ConnDatabase connDatabase = new ConnDatabase();
    Statement stmt = null;
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    
    public void insert(Long vid,String vname,Long pid) throws SQLException{
        Connection conn = DbUtil.getConnection();
        ResultSet rs2=null;
        PreparedStatement pstmt1 = null;
        try{
            pstmt1 = conn.prepareStatement(SQL_INSERT);
            System.out.println("--------------insert--------"+pid);
            pstmt1.setLong(1, vid);
            pstmt1.setString(2, vname);
            pstmt1.setLong(3, pid);
            pstmt1.execute();
            pstmt1.close();
            pstmt1=null;
        } catch(Exception e) {
            e.printStackTrace();
        }
        finally{
            connDatabase.CloseConnection(conn, null, rs2);
        }
    }
    
    public static void main (String args[]) throws SQLException, ApiException{
        PvalueDao pvDao = new PvalueDao();
        CatsDao catsDao = new CatsDao();
        List<Cats> cats = catsDao.getCats();
        Props props = new Props();
        for(Cats cat : cats){
            List<ItemProp> propsList = props.getCatProps(cat.getCid());
            if(null != propsList){
                for(ItemProp itemPorp : propsList){
                    List<PropValue> pValues = itemPorp.getPropValues();
                    if( null != pValues){
                        for(PropValue pv : pValues){
                            pvDao.insert(pv.getVid(), pv.getName(), itemPorp.getPid());
                        }
                    }
                }
            }
        }
    }
    
}
